/**
  ******************************************************************************
  * @file    Libraries/Device/TS32F020D/ts32f020d_system.h
  * @author  JUSHENG Application Team
  * @version V1.0.0
  * @date    02-19-2022
  * @brief   CMSIS Cortex-M0 Device Peripheral Access Layer Header File.
  *          This file contains all the peripheral register's definitions, bits
  *          definitions and memory mapping for TS32F020D Connectivity line.
  *          The file is the unique include file that the application programmer
  *          is using in the C source code, usually in main.c. This file contains:
  *           - Data structures and the address mapping for all peripherals
  *           - Peripheral's registers declarations and bits definition
  *           - Macros to access peripherals registers hardware
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2022 JUSHENG</center></h2>
  *
  *
  *
  ******************************************************************************
  */ 

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __TS32F020D_SYSTEM_H
#define __TS32F020D_SYSTEM_H

#ifdef __cplusplus
 extern "C" {
#endif

/** @addtogroup CMSIS
  * @{
  */

/** @defgroup   CONFIG_SETTING CONFIG_SETTING
  * @brief Various configuration settings
  * @{
  */

/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
#include "typedef.h"
#include "ts32f020d.h"
#include "ts32f020d_selfcheck.h"


/* Private define ------------------------------------------------------------*/
/*! SDK Version V1.0.12    2023-10-30
 */
#define __SDK_VERSION                                       0x010000UL
#define CRC_CHECK_EN                                        1
#define ALL_WORD_FF                                         0xFFFFFFFF
#define ALL_BYTE_FF                                         0xFF
#define ALL_WORD_00                                         0x00000000
/*! Rc calibration clk output to pb3 only for test_chip rc osc is 26M.  
 *  User set rc calibration value to the Associated register,when open the macro USE_RC_TRIM_TEST.  
 *  You must must close this macro ,if it is formal Mass production chip(chip already do it itself),  
 *  or else it is very very serious mistake.  
 */
#define USE_RC_TRIM_TEST                                    0


/* if need using SWD line as common gpio, you need set this macro 1 to disable swd function */
#define SWD_DISABLE    0


#define USING_TS32F020D_CHIP                                1


#define SYS_CLK                                             48000000UL
//#pragma import(__use_no_semihosting) 

#define SELF_CHECK_LEVEL                                    SCL_NONE

#define SCL_NONE                                            0
#define SCL_STARTUP                                         1
#define SCL_RUNTIME                                         2
#define SCL_ALL                                             3

#if (SCL_NONE == SELF_CHECK_LEVEL)
    #define SCL_STARTUP_EN                                  0
    #define SCL_RUNTIME_EN                                  0
#elif (SCL_STARTUP == SELF_CHECK_LEVEL)
    #define SCL_STARTUP_EN                                  1
    #define SCL_RUNTIME_EN                                  0
#elif (SCL_RUNTIME == SELF_CHECK_LEVEL)
    #define SCL_STARTUP_EN                                  0
    #define SCL_RUNTIME_EN                                  1
#elif (SCL_ALL == SELF_CHECK_LEVEL)
    #define SCL_STARTUP_EN                                  1
    #define SCL_RUNTIME_EN                                  1
#endif

#define DEBUG_LEVEL                                         DL_NONE//DL_DGB
#define DMA_DBG_EN                                          0
#define SD_DBG_EN                                           1
#define LCD_DBG_EN                                          0
#define SPI_DBG_EN                                          1
#define IIC_DBG_EN                                          1

/*********************************************************************************/
// system debug level : normal \ warning \ error \ none
/*********************************************************************************/
#define DL_NONE                                             0
#define DL_ERR                                              1
#define DL_WARNING                                          2
#define DL_DGB                                              3

#if (DL_NONE == DEBUG_LEVEL)
    #define DBG_ERR_EN                                      0
    #define DBG_WAR_EN                                      0
    #define DBG_NOR_EN                                      0
#elif (DL_ERR == DEBUG_LEVEL)
    #define DBG_ERR_EN                                      1
    #define DBG_WAR_EN                                      0
    #define DBG_NOR_EN                                      0
#elif (DL_WARNING == DEBUG_LEVEL)
    #define DBG_ERR_EN                                      1
    #define DBG_WAR_EN                                      1
    #define DBG_NOR_EN                                      0
#elif (DL_DGB == DEBUG_LEVEL)
    #define DBG_ERR_EN                                      1
    #define DBG_WAR_EN                                      1
    #define DBG_NOR_EN                                      1
#endif

/* Private macro -------------------------------------------------------------*/
#if (DBG_ERR_EN)
    #define DBG_ERR(fmt, args...)                           printf("[err]");       printf(fmt, ##args)
    #define DBG_ERR_BUF(buf, len)                           printf_array(buf, len)
#else
    #define DBG_ERR(...)
    #define DBG_ERR_BUF(buf, len)
#endif

#if (DBG_WAR_EN)
    #define DBG_WAR(fmt, args...)                           printf("[warning]");   printf(fmt, ##args)
    #define DBG_WAR_BUF(buf, len)                           printf_array(buf, len)
#else
    #define DBG_WAR(...)
    #define DBG_WAR_BUF(buf, len)
#endif

#if (DBG_NOR_EN)
    #define DBG_NOR(fmt, args...)                           printf(fmt, ##args)
    #define DBG_NOR_BUF(buf, len)                           printf_array(buf, len)
#else
    #define DBG_NOR(...)
    #define DBG_NOR_BUF(buf, len)
#endif

/*********************************************************************************/
// define each peripheral debug info
/*********************************************************************************/
#if (DMA_DBG_EN)
    #define DMA_DBG_NOR(fmt, args...)                       DBG_NOR(fmt, ##args)
    #define DMA_DBG_NOR_BUF(buf, len)                       DBG_NOR_BUF(buf, len)
#else
    #define DMA_DBG_NOR(...)
    #define DMA_DBG_NOR_BUF(buf, len)
#endif

#if (SD_DBG_EN)
    #define SD_DBG_NOR(fmt, args...)                        DBG_NOR(fmt, ##args)
    #define SD_DBG_NOR_BUF(buf, len)                        DBG_NOR_BUF(buf, len)
#else
    #define SD_DBG_NOR(...)
    #define SD_DBG_NOR_BUF(buf, len)
#endif

#if (LCD_DBG_EN)
    #define LCD_DBG_NOR(fmt, args...)                       DBG_NOR(fmt, ##args)
    #define LCD_DBG_NOR_BUF(buf, len)                       DBG_NOR_BUF(buf, len)
#else
    #define LCD_DBG_NOR(...)
    #define LCD_DBG_NOR_BUF(buf, len)
#endif

#if (SPI_DBG_EN)
    #define SPI_DBG_NOR(fmt, args...)                       DBG_NOR(fmt, ##args)
    #define SPI_DBG_NOR_BUF(buf, len)                       DBG_NOR_BUF(buf, len)
#else
    #define SPI_DBG_NOR(...)
    #define SPI_DBG_NOR_BUF(buf, len)
#endif

#if (IIC_DBG_EN)
    #define IIC_DBG_NOR(fmt, args...)                       DBG_NOR(fmt, ##args)
    #define IIC_DBG_NOR_BUF(buf, len)                       DBG_NOR_BUF(buf, len)
#else
    #define IIC_DBG_NOR(...)
    #define IIC_DBG_NOR_BUF(buf, len)
#endif

/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

/* Private variables ---------------------------------------------------------*/
/* extern u32 __vector_handlers; */
extern volatile u32 SysTickCnt;

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
  * @brief  Setup the microcontroller system
  *         Initialize the Embedded Flash Interface, the PLL and update the 
  *         SystemCoreClock variable.
  * @note   This function should be used only after reset.
  * @param  None
  * @retval None
  */
void SystemInit(void);
/**
  * @brief  is_systick_expired.
  * @param  offset_ticks: current sys tick 
  * @param  Texpire: offset ticks
  * @retval return true or fault
  */
u32 is_systick_expired(s32 offset_ticks, s32 Texpire);
/**
  * @brief  SystemTickInit.
  * @param  None
  * @retval return true or fault
  */
u32 SystemTickInit(void);
/**
  * @brief  GetSysTick.
  * @param  None
  * @retval return the count of current systick
  */
u32 GetSysTick(void);
/**
  * @brief  sys_clk_hirc.
  * @param  hirc selection
  * @retval None
  */
void sys_clk_hirc(u32 clk);

#if USE_RC_TRIM_TEST
/**
  * @brief  calibration rc 26M for test chip.
  * @param  None
  * @retval return true or fault
  */
void rc_trim_test(void);
#endif
/**
  * @brief  Use the CPU to delay approximately ms
  * @param  n : the number of ms to delay
  * @retval None
  */
void delay_ms(u32 n);
/**
  * @brief  Use the CPU to delay approximately us
  * @param  n : the number of us to delay
  * @retval None
  */
void delay_us(u32 n);

/**
  * @brief sys_special function init
  * @param None
  * @retval None 
  */
void sys_special_fun_init(void);

/**
  * @brief sys_clk function init
  * @param None
  * @retval None 
  */
void sys_clk_init(void);

#ifdef __cplusplus
}
#endif

#endif //__TS32F020D_SYSTEM_H

/*************************** (C) COPYRIGHT 2022 JUSHENG ***** END OF FILE *****/
